﻿using Core;
using Core.ReadAndWrite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class frmNguoiDung : Form
    {

        public frmNguoiDung()
        {
            InitializeComponent();
            dgvKhoa.AutoGenerateColumns = false;
            dgvSinhVien.AutoGenerateColumns = false;
            dgvDKHP.AutoGenerateColumns = false;
        }
        public static TaiKhoan User = null;
        private void LoadKhoa()
        {
            List<Khoa> data = new List<Khoa>();
            foreach (var item in ProxyQuanLi.Instance.QLKhoa.ListKhoa)
            {
                data.Add(item as Khoa);
            }
            dgvKhoa.DataSource = data;
        }
        private void LoadCombobox()
        {
            List<LopHoc> data = new List<LopHoc>();
            foreach (var item in ProxyQuanLi.Instance.QLyLop.data)
            {
                //data.Add(item as LopHoc);
                cbbLop.Items.Add(item as LopHoc);
            }

            //cbbLop.DataSource = data;
            cbbLop.DisplayMember = "TenLop";
            cbbLop.ValueMember = "MaLop";
            // cbbLop.Dispose();
            cbbLop.Items.Insert(0, "Chọn lớp");
            cbbLop.SelectedIndex = 0;

        }
        private void LoadHocPhan()
        {
            List<HocPhan> data = new List<HocPhan>();
            foreach (var item in ProxyQuanLi.Instance.QLHocPhan.ListHocPhan.FindAll(x => (x as HocPhan).HocKy == frmLogin.hocki && (x as HocPhan).NamHoc == frmLogin.namhoc))
            {
                data.Add(item as HocPhan);
            }
            dgvHocPhan.DataSource = data;
        }
        private void LoadDKHocPhan()
        {
            List<DangKiHocPhan> data = new List<DangKiHocPhan>();
            foreach (var item in ProxyQuanLi.Instance.QLDKHocPhan.ListHocPhanDK)
            {
                data.Add(item as DangKiHocPhan);
            }

            //throw new NotImplementedException();
        }
        private void LoadDKHocPhanbyUser()
        {
            if (User != null)
            {
                var data = ProxyQuanLi.Instance.QLDKHocPhan.FindAll(c => (c as DangKiHocPhan).MaSV == User.Username);
                List<DangKiHocPhan> temp = new List<DangKiHocPhan>();
                foreach (var item in data)
                {
                    temp.Add(item as DangKiHocPhan);
                    dgvDKHP.DataSource = temp;
                }
            }
        }
        private void frmNguoiDung_Load(object sender, EventArgs e)
        {
            LoadKhoa();
            LoadHocPhan();
            LoadCombobox();
            LoadDKHocPhan();
            LoadDKHocPhanbyUser();
            LoađKHPofSV();
        }



        private void cbbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            LopHoc sv = cbbLop.SelectedItem as LopHoc;

            if (cbbLop.SelectedIndex > 0)
            {
                var data = ProxyQuanLi.Instance.QLSV.FindAll(c => (c as SinhVien).MaLop ==
                           sv.MaLop);
                LoadSinhVien(data);
            }

        }

        void LoadSinhVien(List<BaseData> dssv)
        {
            List<SinhVien> data = new List<SinhVien>();
            foreach (var item in dssv)
            {
                data.Add(item as SinhVien);
            }
            dgvSinhVien.DataSource = data;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            bool k = false;
            DangKiHocPhan dkhp = new DangKiHocPhan();
            DangKiHocPhan dkhp1 = (DangKiHocPhan)QuanLiDKHP.Instance.Find(x => (x as DangKiHocPhan).NamHoc == frmLogin.namhoc && (x as DangKiHocPhan).HocKi == frmLogin.hocki
              && (x as DangKiHocPhan).MaSV == frmNguoiDung.User.Username);

            dkhp.MaSV = frmNguoiDung.User.Username;
            for (int i = 0; i < dgvHocPhan.RowCount; i++)
            {
                if (Convert.ToBoolean(dgvHocPhan[0, i].Value) == true)
                {

                    HocPhan hp = (HocPhan)QuanLiDSHocPhan.Instance.Find(x => (x as HocPhan).MaHocPhan == dgvHocPhan[1, i].Value);
                    if (dkhp1 == null)
                    {
                        if (hp != null)
                        {
                            if (!k)
                            {
                                dkhp.NamHoc = hp.NamHoc;
                                dkhp.HocKi = hp.HocKy;
                                k = true;
                            }

                            dkhp.AddHocPhan(hp);

                        }
                    }
                    else
                    {
                        dkhp1.AddHocPhan(hp);
                    }


                }


            }
            QuanLiDKHP.Instance.Add(dkhp);
            DataSource.Instance.WriteTo(QuanLiDKHP.Instance.ToString(), "QLDKHP.txt");
            LoađKHPofSV();
        }

        private void LoađKHPofSV()
        {
            DangKiHocPhan dkhp = (DangKiHocPhan)QuanLiDKHP.Instance.Find(x => (x as DangKiHocPhan).NamHoc == frmLogin.namhoc && (x as DangKiHocPhan).HocKi == frmLogin.hocki
                && (x as DangKiHocPhan).MaSV == frmNguoiDung.User.Username) ?? new DangKiHocPhan();
            List<HocPhan> ds1 = new List<HocPhan>();
            foreach (var item in dkhp.HocPhanThamGia_Diem)
            {
                ds1.Add(item.HocPhanTheoHoc);

            }
            dgvDKHP.DataSource = ds1;
        }

        private void dgvDKHP_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dgvDKHP.RowCount; i++)
                {
                    if (Convert.ToBoolean(dgvDKHP[3, i].Value) == true)
                    {
                        DangKiHocPhan dkhp = (DangKiHocPhan)QuanLiDKHP.Instance.Find(x => (x as DangKiHocPhan).NamHoc == frmLogin.namhoc && (x as DangKiHocPhan).HocKi == frmLogin.hocki
                    && (x as DangKiHocPhan).MaSV == frmNguoiDung.User.Username);

                        if (dkhp != null)
                        {
                            dkhp.RemoveHocPhan(dgvDKHP[0, i].Value + "");
                        }
                    }
                }
                DataSource.Instance.WriteTo(QuanLiDKHP.Instance.ToString(), "QLDKHP.txt");
                LoađKHPofSV();
            }
            catch (Exception)
            {


            }


        }

    }
}
